package hehe.container.definition;

import hehe.container.build.xml.tag.Tag;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * bean 定义管理类
 * <B>说明：</B>
 * <pre>
 *  管理全部bean定义
 * </pre>
 * <B>示例：</B>
 * <pre>
 *  略
 * </pre>
 * <B>日志：</B>
 * <pre>
 *  略
 * </pre>
 * <B>注意事项：</B>
 * <pre>
 *  略
 * </pre>
 */
public class BeanDefinitionManger
{
    /**
     * bean 定义列表
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    protected Map<String,BeanDefinition> beanDefinitions = new HashMap<>();


    /**
     * 判断是否有bean 定义
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     * @param beanId beanId
     */
    public boolean hasBeanDefinition(String beanId)
    {
        if (this.beanDefinitions.containsKey(beanId)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取bend 定义
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     * @param beanId beanId
     */
    public BeanDefinition getBeanDefinition(String beanId)
    {
        if (this.beanDefinitions.containsKey(beanId)) {
            return this.beanDefinitions.get(beanId);
        } else {
            return null;
        }
    }

    /**
     * 获取bend 定义
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     * @param clazz beanId
     */
    public BeanDefinition getBeanDefinition(Class clazz)
    {
        String beanId = clazz.getName();
        if (!this.beanDefinitions.containsKey(beanId)) {
            BeanDefinition beanDefinition = new BeanDefinition(beanId);
            this.beanDefinitions.put(beanDefinition.getId(),beanDefinition);
        }

        return this.beanDefinitions.get(beanId);
    }

    /**
     * 查找Bean定义
     *<B>说明：</B>
     *<pre>
     * 略
     *</pre>
     * @param clazz 目标类
     * @param noFindCreate 查找不到　是否创建新bean
     */
    public BeanDefinition findBeanDefinition(Class clazz,boolean noFindCreate)
    {
        BeanDefinition beanDefinition = this.searchBeanDefinition(clazz);
        if (beanDefinition == null && noFindCreate == true) {
            beanDefinition = this.registerBeanDefinition(clazz);
        }

        return beanDefinition;
    }

    /**
     * 搜索BeanDefinition
     *<B>说明：</B>
     *<pre>
     *  用于自动装配对象
     *</pre>
     */
    public BeanDefinition searchBeanDefinition(Class clazz)
    {
        // 先查找按id,匹配,如找不到,遍历目前BeanDefinition　列表,安class 查找
        String beanId = clazz.getName();
        if (this.beanDefinitions.containsKey(beanId)) {
            return this.beanDefinitions.get(beanId);
        }

        BeanDefinition findBeanDefinition = null;
        BeanDefinition beanDefinition;
        Iterator entries = this.beanDefinitions.entrySet().iterator();

        while (entries.hasNext()) {
            Map.Entry entry = (Map.Entry) entries.next();
            beanDefinition = (BeanDefinition)entry.getValue();
            if (beanDefinition.getRef().equals(beanId)) {
                findBeanDefinition = beanDefinition;
                break;
            }
        }

        return findBeanDefinition;
    }

    /**
     * 注册Bean定义
     *<B>说明：</B>
     *<pre>
     * 略
     *</pre>
     */
    public BeanDefinition registerBeanDefinition(Class clazz)
    {
        String beanId = clazz.getName();
        Map<String,Object> attrs = new HashMap<>();
        attrs.put(Tag.REF_ATTRIBUTE,beanId);
        attrs.put(Tag.ID_ATTRIBUTE,beanId);

        return  this.registerBeanDefinition(attrs);
    }

    /**
     * 注册Bean定义
     *<B>说明：</B>
     *<pre>
     * 略
     *</pre>
     */
    public BeanDefinition registerBeanDefinition(String ref)
    {
        Map<String,Object> attrs = new HashMap<>();
        attrs.put(Tag.REF_ATTRIBUTE,ref);
        attrs.put(Tag.ID_ATTRIBUTE,ref);

        return  this.registerBeanDefinition(attrs);
    }

    /**
     * 注册Bean定义
     *<B>说明：</B>
     *<pre>
     * 略
     *</pre>
     */
    public BeanDefinition registerBeanDefinition(String beanId,String ref)
    {
        Map<String,Object> attrs = new HashMap<>();
        attrs.put(Tag.REF_ATTRIBUTE,ref);
        attrs.put(Tag.ID_ATTRIBUTE,beanId);

        return  this.registerBeanDefinition(attrs);
    }

    /**
     * 注册bean定义
     *<B>说明：</B>
     *<pre>
     * 属性详细描述
     *</pre>
     * @param attr
     */
    public BeanDefinition registerBeanDefinition(Map<String,Object> attr)
    {
        BeanDefinition beanDefinition = new BeanDefinition(attr);
        this.registerBeanDefinition(beanDefinition);
        return beanDefinition;
    }

    /**
     * 注册bean定义
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     * @param beanDefinition
     */
    public void registerBeanDefinition(BeanDefinition beanDefinition)
    {
        this.beanDefinitions.put(beanDefinition.getId(),beanDefinition);
    }
}
